using System.Collections.ObjectModel;
using CommunityToolkit.Mvvm.ComponentModel;
using AvaloniaUiKit.SampleApp.Models;
using AvaloniaUiKit.SampleApp.Services;
using AvaloniaUiKit.SampleApp.ViewModels.Pages;

namespace AvaloniaUiKit.SampleApp.ViewModels
{
    public partial class MainWindowViewModel : ViewModelBase
    {
        private readonly INavigationService _navigationService;

        [ObservableProperty]
        private NavigationItem _selectedItem = null!;

        [ObservableProperty]
        private string _componentTitle = "Home";

        [ObservableProperty]
        private string _componentDescription = "Showcase of all available UI controls in one view";

        [ObservableProperty]
        private IPageViewModel _currentPage;

        public ObservableCollection<NavigationItem> NavigationItems { get; } = new();

        public bool IsHomeSelected => CurrentPage is HomePageViewModel;

        public MainWindowViewModel(INavigationService navigationService = null!)
        {
            _navigationService = navigationService ?? new NavigationService();
            _currentPage = _navigationService.CurrentPage;
            InitializeNavigationItems();
        }

        partial void OnSelectedItemChanged(NavigationItem value)
        {
            if (value != null && !value.IsCategory)
            {
                ComponentTitle = value.Title;
                // Set description based on selected component
                ComponentDescription = value.Title switch
                {
                    "Home" => "Showcase of all available UI controls in one view",
                    "Buttons" => "Customizable button components with various states and styles",
                    "Cards" => "Interactive card components with animations and visual effects",
                    "Grids" => "Flexible grid layout components for organizing content",
                    "Panels" => "Container panels with various styling options",
                    "FlexWrapPanel" => "灵活的自动换行布局面板，支持水平和垂直方向自动排列",
                    "Text Fields" => "Input components for text entry with validation",
                    "Selection" => "Components for selecting from multiple options",
                    "Dialogs" => "Modal dialog components for user interaction",
                    "Notifications" => "Notification components for user feedback",
                    "MvvmDialogs" => "MVVM-friendly dialog management with MvvmDialogs",
                    _ => "Component description"
                };

                // Navigate to the selected page
                _navigationService.Navigate(value.Title);
                CurrentPage = _navigationService.CurrentPage;
            }
        }

        private void InitializeNavigationItems()
        {
            // Add Home navigation item at the top
            NavigationItems.Add(new NavigationItem("Home", false, "M10 20v-6h4v6h5v-8h3L12 3 2 12h3v8z"));

            // Create Basic Controls category
            var basicControls = new NavigationItem("Basic Controls", true);
            basicControls.Children.Add(new NavigationItem("Buttons"));
            basicControls.Children.Add(new NavigationItem("Cards"));
            NavigationItems.Add(basicControls);

            // Create Layout Components category
            var layoutComponents = new NavigationItem("Layout Components", true);
            layoutComponents.Children.Add(new NavigationItem("Grids"));
            layoutComponents.Children.Add(new NavigationItem("Panels"));
            layoutComponents.Children.Add(new NavigationItem("FlexWrapPanel"));
            NavigationItems.Add(layoutComponents);

            // Create Input Controls category
            var inputControls = new NavigationItem("Input Controls", true);
            inputControls.Children.Add(new NavigationItem("Text Fields"));
            inputControls.Children.Add(new NavigationItem("Selection"));
            NavigationItems.Add(inputControls);

            // Create Feedback category
            var feedback = new NavigationItem("Feedback", true);
            feedback.Children.Add(new NavigationItem("Dialogs"));
            feedback.Children.Add(new NavigationItem("Notifications"));
            NavigationItems.Add(feedback);

            // Create MvvmDialogs category
            var mvvmDialogs = new NavigationItem("MvvmDialogs", true);
            mvvmDialogs.Children.Add(new NavigationItem("MvvmDialogs"));
            NavigationItems.Add(mvvmDialogs);

            // Set the default selected item to Home
            SelectedItem = NavigationItems[0];
        }
    }
}